/* 
 * Copyright 2008 Rodrigo Reyes <reyes.rr at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
**/
package net.kornr.abstractcanvas.client.gwt;



import java.util.Iterator;
import java.util.LinkedList;

import net.kornr.abstractcanvas.client.ICanvas;

import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.ClickListenerCollection;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.MouseListenerCollection;
import com.google.gwt.user.client.ui.MouseWheelListener;
import com.google.gwt.user.client.ui.MouseWheelListenerCollection;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.widgetideas.graphics.client.CanvasGradient;
import com.google.gwt.widgetideas.graphics.client.Color;
import com.google.gwt.widgetideas.graphics.client.GWTCanvas;

public class CanvasPanel extends AbsolutePanel implements ICanvas
{
	private MouseListenerCollection mouseListeners;
	private MouseWheelListenerCollection mouseWheelListeners;
	private ClickListenerCollection clickListeners;

	protected FocusGWTCanvas m_canvas;
	private LinkedList<HTML> m_texts = new LinkedList<HTML>(); 
	
	public CanvasPanel(int width, int height)
	{
		m_canvas = new FocusGWTCanvas(width, height);
		this.add(m_canvas,0,0);
		this.setWidth(""+width+"px");
		this.setHeight(""+height+"px");
		
		this.addMouseListener(new MouseListener() {
			public void onMouseDown(Widget sender, int x, int y) {
			}
			public void onMouseEnter(Widget sender) {
			}
			public void onMouseLeave(Widget sender) {
			}
			public void onMouseMove(Widget sender, int x, int y) {
			}
			public void onMouseUp(Widget sender, int x, int y) {
			}
		});
	}

	public GWTCanvas getGWTCanvas()
	{
		return m_canvas;
	}


	public void arc(double x, double y, double radius, double startAngle,
			double endAngle, boolean antiClockwise) {
		// TODO Auto-generated method stub
		m_canvas.arc(x, y, radius, startAngle, endAngle, antiClockwise);
	}

	public void beginPath() 
	{
		m_canvas.beginPath();
	}

	public void clear() 
	{
		// TODO Auto-generated method stub
		m_canvas.clear();
		for (Iterator<HTML>i=m_texts.iterator(); i.hasNext(); )
		{
			this.remove(i.next());
		}
		m_texts.clear();
	}

	public void closePath() 
	{
		// TODO Auto-generated method stub
		m_canvas.closePath();
	}


	public CanvasGradient createLinearGradient(double x0, double y0, double x1, double y1) 
	{
		// TODO Auto-generated method stub
		return m_canvas.createLinearGradient(x0, y0, x1, y1);
	}


	public CanvasGradient createRadialGradient(double x0, double y0, double r0,
			double x1, double y1, double r1) {
		// TODO Auto-generated method stub
		return m_canvas.createRadialGradient(x0, y0, r0, x1, y1, r1);
	}


	public void cubicCurveTo(double cp1x, double cp1y, double cp2x,
			double cp2y, double x, double y) {
		// TODO Auto-generated method stub
		m_canvas.cubicCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
	}


	public void drawImage(ImageElement img, double offsetX, double offsetY,
			double width, double height) {
		// TODO Auto-generated method stub
		m_canvas.drawImage(img, offsetX, offsetY, width, height);
	}


	public void drawImage(ImageElement img, double offsetX, double offsetY) {
		// TODO Auto-generated method stub
		m_canvas.drawImage(img, offsetX, offsetY);
	}


	public void fill() {
		// TODO Auto-generated method stub
		m_canvas.fill();
	}


	public void fillRect(double startX, double startY, double width,
			double height) {
		// TODO Auto-generated method stub
		m_canvas.fillRect(startX, startY, width, height);
	}


	public int getCoordHeight() {
		// TODO Auto-generated method stub
		return m_canvas.getCoordHeight();
	}


	public int getCoordWidth() {
		// TODO Auto-generated method stub
		return m_canvas.getCoordWidth();
	}


	public double getGlobalAlpha() {
		// TODO Auto-generated method stub
		return m_canvas.getGlobalAlpha();
	}


	public String getGlobalCompositeOperation() {
		// TODO Auto-generated method stub
		return m_canvas.getGlobalCompositeOperation();
	}


	public String getLineCap() {
		// TODO Auto-generated method stub
		return m_canvas.getLineCap();
	}


	public String getLineJoin() {
		// TODO Auto-generated method stub
		return m_canvas.getLineJoin();
	}


	public double getLineWidth() {
		// TODO Auto-generated method stub
		return m_canvas.getLineWidth();
	}


	public double getMiterLimit() {
		// TODO Auto-generated method stub
		return m_canvas.getMiterLimit();
	}

	public void lineTo(double x, double y) {
		// TODO Auto-generated method stub
		m_canvas.lineTo(x, y);
	}


	public void moveTo(double x, double y) {
		// TODO Auto-generated method stub
		m_canvas.moveTo(x, y);
	}


	public void quadraticCurveTo(double cpx, double cpy, double x, double y) {
		// TODO Auto-generated method stub
		m_canvas.quadraticCurveTo(cpx, cpy, x, y);
	}


	public void rect(double startX, double startY, double width, double height) {
		// TODO Auto-generated method stub
		m_canvas.rect(startX, startY, width, height);
	}


	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		this.setWidth(""+width+"px");
		this.setHeight(""+height+"px");
		m_canvas.resize(width, height);
	}


	public void restoreContext() {
		// TODO Auto-generated method stub
		m_canvas.restoreContext();
	}


	public void rotate(double angle) {
		// TODO Auto-generated method stub
		m_canvas.rotate(angle);
	}


	public void saveContext() {
		// TODO Auto-generated method stub
		m_canvas.saveContext();
	}


	public void scale(double x, double y) {
		// TODO Auto-generated method stub
		m_canvas.scale(x, y);
	}


	public void setBackgroundColor(Color color) {
		// TODO Auto-generated method stub
		m_canvas.setBackgroundColor(color);
	}


	public void setCoordHeight(int height) {
		// TODO Auto-generated method stub
		m_canvas.setCoordHeight(height);
	}


	public void setCoordSize(int width, int height) {
		// TODO Auto-generated method stub
		m_canvas.setCoordSize(width, height);
	}


	public void setCoordWidth(int width) {
		// TODO Auto-generated method stub
		m_canvas.setCoordWidth(width);
	}


	public void setFillStyle(CanvasGradient grad) {
		// TODO Auto-generated method stub
		m_canvas.setFillStyle(grad);
	}


	public void setFillStyle(Color color) {
		// TODO Auto-generated method stub
		m_canvas.setFillStyle(color);
	}


	public void setGlobalAlpha(double alpha) {
		// TODO Auto-generated method stub
		m_canvas.setGlobalAlpha(alpha);
	}


	public void setGlobalCompositeOperation(String globalCompositeOperation) {
		// TODO Auto-generated method stub
		m_canvas.setGlobalCompositeOperation(globalCompositeOperation);
	}


	public void setLineCap(String lineCap) {
		// TODO Auto-generated method stub
		m_canvas.setLineCap(lineCap);
	}


	public void setLineJoin(String lineJoin) {
		// TODO Auto-generated method stub
		m_canvas.setLineJoin(lineJoin);
	}


	public void setLineWidth(double width) {
		// TODO Auto-generated method stub
		m_canvas.setLineWidth(width);
	}


	public void setMiterLimit(double miterLimit) {
		// TODO Auto-generated method stub
		m_canvas.setMiterLimit(miterLimit);
	}


	public void setPixelHeight(int height) {
		// TODO Auto-generated method stub
		m_canvas.setPixelHeight(height);
	}


	public void setPixelWidth(int width) {
		// TODO Auto-generated method stub
		m_canvas.setPixelWidth(width);
	}


	public void setStrokeStyle(CanvasGradient grad) {
		// TODO Auto-generated method stub
		m_canvas.setStrokeStyle(grad);
	}


	public void setStrokeStyle(Color color) {
		// TODO Auto-generated method stub
		m_canvas.setStrokeStyle(color);
	}


	public void stroke() {
		// TODO Auto-generated method stub
		m_canvas.stroke();
	}


	public void strokeRect(double startX, double startY, double width,
			double height) {
		// TODO Auto-generated method stub
		m_canvas.strokeRect(startX, startY, width, height);
	}


	public void transform(double m11, double m12, double m21, double m22,
			double dx, double dy) {
		// TODO Auto-generated method stub
		m_canvas.transform(m11, m12, m21, m22, dx, dy);
	}


	public void translate(double x, double y) {
		// TODO Auto-generated method stub
		m_canvas.translate(x, y);
	}

	public void addClickListener(ClickListener listener) 
	{
		if (clickListeners == null) {
			clickListeners = new ClickListenerCollection();
		}
		clickListeners.add(listener);
		m_canvas.addClickListener(listener);
	}
	public void addMouseListener(MouseListener listener) 
	{
		if (mouseListeners == null) {
			mouseListeners = new MouseListenerCollection();
		}
		mouseListeners.add(listener);
		m_canvas.addMouseListener(listener);
	}

	public void addMouseWheelListener(MouseWheelListener listener) 
	{
		if (mouseWheelListeners == null) {
			mouseWheelListeners = new MouseWheelListenerCollection();
		}
		mouseWheelListeners.add(listener);
		m_canvas.addMouseWheelListener(listener);
	}


	public void removeClickListener(ClickListener listener) 
	{
		if (clickListeners != null) {
			clickListeners.remove(listener);
		}
		m_canvas.removeClickListener(listener);
	}

	public void removeMouseListener(MouseListener listener) 
	{
		if (mouseListeners != null) {
			mouseListeners.remove(listener);
		}
		m_canvas.removeMouseListener(listener);
	}

	public void removeMouseWheelListener(MouseWheelListener listener) 
	{
		if (mouseWheelListeners != null) {
			mouseWheelListeners.remove(listener);
		}
		m_canvas.addMouseWheelListener(listener);
	}

}
